package io.renren.modules.device.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.service.SysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.modules.device.entity.BuildingEntity;
import io.renren.modules.device.service.BuildingService;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 大楼
 *
 * @author lwm
 * @email 
 * @date 2019-01-08 10:55:54
 */
@RestController
@RequestMapping("device/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("device:building:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = buildingService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("device:building:info")
    public R info(@PathVariable("id") Long id){
			BuildingEntity building = buildingService.selectById(id);

			if(building.getDeptId()!=null){
				SysDeptEntity dept = sysDeptService.selectById(building.getDeptId());
				if(dept!=null){
					building.setDeptName(dept.getName());
				}
			}
        return R.ok().put("building", building);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("device:building:save")
    public R save(@RequestBody BuildingEntity building){
    	verifyForm(building);
    	building.setCreateTime(new Date());
		buildingService.insert(building);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("device:building:update")
    public R update(@RequestBody BuildingEntity building){
    	if(!StringUtils.hasLength(building.getBuildingCode())) {
			throw new RRException("大楼编号不能为空");
		}
    	if(!StringUtils.hasLength(building.getProvinces())){
			throw new RRException("省不能为空");
		}
		if(!StringUtils.hasLength(building.getCities())){
			throw new RRException("市不能为空");
		}
		if(!StringUtils.hasLength(building.getCounties())){
			throw new RRException("县不能为空");
		}
		if(!StringUtils.hasLength(building.getBuildingName())){
			throw new RRException("楼名不能为空");
		}
		buildingService.updateById(building);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("device:building:delete")
    public R delete(@RequestBody Long[] ids){
			buildingService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
	 * 验证参数是否正确
	 */
	private void verifyForm(BuildingEntity building){
		if(!StringUtils.hasLength(building.getBuildingCode())) {
			throw new RRException("大楼编号不能为空");
		}
		int value = buildingService.selectCount(new EntityWrapper<BuildingEntity>().eq("BUILDING_CODE", building.getBuildingCode()));
		if(value > 0) {
			throw new RRException("该大楼编号已经存在");
		}
		if(!StringUtils.hasLength(building.getProvinces())){
			throw new RRException("省不能为空");
		}
		if(!StringUtils.hasLength(building.getCities())){
			throw new RRException("市不能为空");
		}
		if(!StringUtils.hasLength(building.getCounties())){
			throw new RRException("县不能为空");
		}
		if(!StringUtils.hasLength(building.getBuildingName())){
			throw new RRException("楼名不能为空");
		}
	}
    
}
